package view.graph;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.PickingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.PluggableGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.TranslatingGraphMousePlugin;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class GraphFrame {
	private JFrame frame;
	private JPanel paint; 
	public GraphFrame(JFrame outer) {
		frame = new JFrame("GraphFrame");
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("icon/model.png ");
		frame.setIconImage(image);
		frame.setLocationRelativeTo(outer);
		paint = new JPanel();
	}
	public void launchFrame() {
		frame.setPreferredSize(new Dimension(600, 450));
		frame.setResizable(false);
		frame.pack();
		//strucher
		DirectedSparseMultigraph graph = new DirectedSparseMultigraph<String, String>();
		String z1 = "z12 / y12";
		String z2 = "z2";
		graph.addVertex(z1);
		graph.addVertex(z2);
		graph.addEdge("link",z1, z2);
		
		//view
		Layout<String, String> layout = new CircleLayout(graph);
		layout.setSize(new Dimension(400, 400));
		VisualizationViewer<String, String> vv = new VisualizationViewer<String, String>(layout);
		vv.setPreferredSize(new Dimension(700, 500));
		//str=node
		Transformer<String, Paint> vertexPaint = new Transformer<String, Paint>() {
			public Paint transform(Integer i) {
				return Color.GREEN;
			}

			@Override
			//str=node
			public Paint transform(String arg0) {
				return Color.GREEN;
			}
		};
		Transformer<String,Shape> vertexSize = new Transformer<String,Shape>(){
            public Shape transform(String i){
                Ellipse2D circle = new Ellipse2D.Double(-15, -15, 55, 45);
                	return circle;
            }
        };
        vv.getRenderContext().setVertexShapeTransformer(vertexSize);
		vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
		vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
		vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
		vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
		vv.setBackground(Color.white);
		
		PluggableGraphMouse pgm = new PluggableGraphMouse();
        pgm.add(new PickingGraphMousePlugin());
        pgm.add(new TranslatingGraphMousePlugin(MouseEvent.BUTTON3_MASK));
        pgm.add(new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0, 1 / 1.1f, 1.1f));
        vv.setGraphMouse(pgm);
        
		
		paint.setBackground(Color.white);
		paint.add(vv);
		JScrollPane scroll = new JScrollPane(paint);
		frame.getContentPane().add(scroll);
		paint.repaint();
		frame.setVisible(true);
	}
	public static void main(String[] args) {
		GraphFrame frame = new GraphFrame(null);
		frame.launchFrame();
	}

}
